clear all
set more off
set type double, perm
capture log close

global d = 0

if $d == 0 { 
global topdir "YOUR DIRECTORY"
global datadir "${topdir}/data"
global outdir "${topdir}/output"
cd "${outdir}"
}

use sample group group2 female wt math ln_hrinc using "${datadir}/nels_final.dta", clear
keep if math!=.

log using nels_yitzhaki.log, replace


/*  ------------------------------------------------------------------------  */
/*               Data Preparation for Yitzhaki Decomposition                  */
/*  ------------------------------------------------------------------------  */

// grouped
sort sample female math
by sample female math: egen wt_groupsize = total(wt) // group size including sample weight
by sample female math: egen sum_ln_wage = total(ln_hrinc*wt)
gen mean_ln_wage = sum_ln_wage/wt_groupsize // weighted avg log wage

// tag
egen tag = tag(sample female math)

// ols
foreach g in WM WW {
	reg ln_hrinc math if sample==0 & group2=="`g'" [w=wt]
	scalar ols_wt_math_`g'88 = round(_b[math],0.001)
	reg ln_hrinc math if sample==1 & group2=="`g'" [w=wt]
	scalar ols_wt_math_`g'02 = round(_b[math],0.001)
}

// save
sort sample female math
tempfile rawdata
save `rawdata'



/*  ------------------------------------------------------------------------  */
/*               Decomposition by Race/Ethnicity and Gender                   */
/*  ------------------------------------------------------------------------  */

// collapse
use `rawdata'
keep if tag==1
keep sample female wt_groupsize mean_ln_wage math 

// calculate beta for each observation
sort sample female math
by sample female: gen b = (mean_ln_wage[_n+1]-mean_ln_wage[_n])/(math[_n+1]-math[_n])

// Yitzhaki weight
** incorporating multiple obs per X and sample weights
sort sample female math
by sample female: gen rsumN = sum(wt_groupsize) // running sum of group size ("weight")
by sample female: egen sumN = total(wt_groupsize) // total sum of group size
by sample female: gen rsumNh = sum(wt_groupsize*math) // running sum of group size * X
by sample female: egen sumNh = total(wt_groupsize*math) // total sum of group size * X
by sample female: gen mean_math = sumNh/sumN // (weighted) sample mean
by sample female: egen var_math = total(wt_groupsize*(math-mean_math)^2*(1/sumN)) // (weighted) sample variance
by sample female: gen term1 = (1/var_math) * (rsumN/sumN) * ((sumN-rsumN)/sumN)
by sample female: gen term2 = (sumNh - rsumNh)/(sumN - rsumN) - rsumNh/rsumN
by sample female: gen term3 = math[_n+1] - math[_n]
gen ols_weight = term1 * term2 * term3

drop rsumN sumN rsumNh sumNh mean_math var_math term1 term2 term3


// cumulative contribution
sort sample female math
gen bw = b*ols_weight
by sample female: gen ols_run_bw = sum(bw)

// save
keep sample female math b ols_weight ols_run_bw 
sort sample female math
tempfile full
save `full'

// merge back
use `rawdata'
sort sample female math
merge m:1 sample female math using `full', nogen



/*  ------------------------------------------------------------------------  */
/*                             Figure C1                                      */
/*  ------------------------------------------------------------------------  */

twoway (lowess mean_ln_wage math if sample==0 & group2=="WM", lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage math if sample==1 & group2=="WM", lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_weight math if tag==1 & sample==0 & group2=="WM", lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_weight math if tag==1 & sample==1 & group2=="WM", lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NELS-88: Log wage" 2 "ELS-02: Log wage" 3 "NELS-88: Weight" 4 "ELS-02: Weight")) scheme(s1color) ///
	xtitle("math score") ytitle("Log hourly earnings") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(12 88)) xlabel(15(10)85) aspectratio(.8) title("White Non-Hispanic Men") name(g1,replace)
twoway (lowess mean_ln_wage math if sample==0 & group2=="WW", lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage math if sample==1 & group2=="WW", lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_weight math if tag==1 & sample==0 & group2=="WW", lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_weight math if tag==1 & sample==1 & group2=="WW", lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NELS-88: Log wage" 2 "ELS-02: Log wage" 3 "NELS-88: Weight" 4 "ELS-02: Weight")) scheme(s1color) ///
	xtitle("math score") ytitle("Log hourly earnings") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(12 88)) xlabel(15(10)85) aspectratio(.8) title("White Non-Hispanic Women") name(g2,replace)
grc1leg g1 g2, legendfrom(g1) ycommon col(2) scheme(s1color) ysize(2) xsize(3.5)
graph export FigureC1.pdf, replace
graph export FigureC1.eps, replace
graph save FigureC1, replace


/*  ------------------------------------------------------------------------  */
/*                             Figure C2                                      */
/*  ------------------------------------------------------------------------  */

foreach g in WM WW {
	local ols`g'88 = scalar(ols_wt_math_`g'88)
	local ols`g'02 = scalar(ols_wt_math_`g'02)
}

twoway (lowess ols_run_bw math if tag==1 & sample==0 & group2=="WM", bw(0.3)) ///
	(lowess ols_run_bw math if tag==1 & sample==1 & group2=="WM", bw(0.3)), ///
	legend(order(1 "NELS-88" 2 "ELS-02")) xtitle("math score") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM88',lc(dkgreen) lp(dot)) yline(`olsWM02',lc(orange_red) lp(dot)) ///
	xsc(r(12 88)) xlabel(15(10)85) scheme(s1color) aspectratio(.8) title("White Non-Hispanic Men") name(g1,replace)
twoway (lowess ols_run_bw math if tag==1 & sample==0 & group2=="WW", bw(0.3)) ///
	(lowess ols_run_bw math if tag==1 & sample==1 & group2=="WW", bw(0.3)), ///
	legend(order(1 "NELS-88" 2 "ELS-02")) xtitle("math score") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW88',lc(dkgreen) lp(dot)) yline(`olsWW02',lc(orange_red) lp(dot)) ///
	xsc(r(12 88)) xlabel(15(10)85) scheme(s1color) aspectratio(.8) title("White Non-Hispanic Women") name(g2,replace)
grc1leg g1 g2, legendfrom(g1) ycommon col(2) scheme(s1color)
graph export FigureC2.pdf, replace
graph export FigureC2.eps, replace
graph save FigureC2, replace





log close
exit





